package com.example.sinoyd.frameapplication.ProjectCodes.Fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeytime
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.GVkeyvalue
import com.example.sinoyd.frameapplication.KotlinFrame.Dataclass.gson
import com.example.sinoyd.frameapplication.KotlinFrame.Frame.Uitl.Networkrequestmodel
import com.example.sinoyd.frameapplication.KotlinFrame.Uitl.showdialog
import com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetOnlineInfo
import com.example.sinoyd.frameapplication.R
import com.sinoyd.Code.Until.Networkrequestaddress
import com.sinoyd.Code.Until.SharedPreferencesFactory
import kotlinx.android.synthetic.main.include_title_layout.*
import kotlinx.android.synthetic.main.online_fragment.*
import okhttp3.Response
import org.jetbrains.anko.act
import org.jetbrains.anko.runOnUiThread
import org.jetbrains.anko.toast

/**
 * 在线情况界面
 * **/
@SuppressLint("ValidFragment")
class OnlineFragment(var name: String = "在线情况") : BaseFragment() {
    //选择站点后进行的操作
    override fun selectPortCallBack() {
        toast("选择站点后一系列请求")
    }


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val conview = inflater.inflate(R.layout.online_fragment, null)
        return conview
    }

    override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setview()
        //发送在线情况请求
        GetOnlineInfo()
    }


    //设置界面
    private fun setview() {
        center_title.text = name
        iv_left_one.visibility = View.INVISIBLE
        iv_left_two.visibility = View.INVISIBLE
    }


    var getOnlineInfo: GetOnlineInfo = com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetOnlineInfo()
    //网络请求返回处理
    override fun requestSuccess(response: Response, TAG: String) {
        super.requestSuccess(response, TAG)
        when (TAG) {
            "GetOnlineInfo" -> {
                getOnlineInfo = gson.fromJson(responsestr, com.example.sinoyd.frameapplication.ProjectCodes.Dataclass.GetOnlineInfo::class.java)
                //整理数据
                if (getOnlineInfo.onlineInfo.size > 0) {
                    var list: ArrayList<GVkeyvalue> = ArrayList()
                    for (item in getOnlineInfo.onlineInfo) {
                        list.add(GVkeyvalue(GVkeytime(item.pointName, item.pointId), GVkeytime("状态", ""), item.status))
                        list.add(GVkeyvalue(GVkeytime(item.pointName, item.pointId), GVkeytime("记录条数", ""), item.recordCount))
                        list.add(GVkeyvalue(GVkeytime(item.pointName, item.pointId), GVkeytime("最近数据时间", ""), item.lastestTime))
                    }
                    //显示二维图表
                    runOnUiThread {
                        twolistview.initview("点位", false, false, 110f).setdata(list).show()
                    }
                } else {
//                    Looper.prepare()
//                    toast("未获取到在线情况数据")
//                    Looper.loop()
                    var list: ArrayList<GVkeyvalue> = ArrayList()
                    runOnUiThread {
                        twolistview.initview("点位", false, false, 110f).setdata(list).show()
                    }
                }

            }
        }
    }


    //获取在线情况
    fun GetOnlineInfo() {
        showdialog(act, "loadshow")
        var ntrequest = Networkrequestmodel()
        ntrequest.setMethod(Networkrequestmodel.GETREQUEST)
                .settag("GetOnlineInfo")
                .seturl(Networkrequestaddress.URL_GetOnlineInfo)
                .addparam("sysType", SharedPreferencesFactory.getdata(act, "OS"))
                .start(this)
    }
}